'use strict';
goog.provide('Blockly.Blocks.Rais_MixlyLib');
goog.require('Blockly.Blocks');

Blockly.Blocks.Rais_MixlyLib.Colour			= "#9E9E9E";
Blockly.Blocks.Rais_MixlyLib.LinkedListColour = "#AACCEE";
Blockly.Blocks.Rais_MixlyLib.MatrixColour	= "#F48FB1";
Blockly.Blocks.Rais_MixlyLib.GraphColour	= "#9BCD9B";
Blockly.Blocks.Rais_MixlyLib.LineTrackerColour = "#7986CB";
Blockly.Blocks.Rais_MixlyLib.UtilsColour		= "#EECFA1";
Blockly.Blocks.Rais_MixlyLib.EightButtonsColour = "#ff5252";

//Rais_DuXingS&T -------------------------------------------------------------//

var motor_array = [[Blockly.M1 ,"M1"], [Blockly.M2 ,"M2"]];
var wheels_array = [[Blockly.M1_M2, "true"], [Blockly.M2_M1, "false"]];
var wheel_LR_array = [[Blockly.LWheel, "L"], [Blockly.RWheel, "R"]];
var wheel_rotation_array = [[Blockly.CLOCKWISE, 'CLOCKWISE'], [Blockly.ANTI_CLOCKWISE, 'ANTI_CLOCKWISE']];

Blockly.Blocks.run_motor = {
	init: function() {
		
		this.setColour(Blockly.Blocks.Rais_MixlyLib.Colour);    
	
		this.appendDummyInput("")	
			.appendField(Blockly.RUN_MOTOR)
			.appendField(new Blockly.FieldDropdown(motor_array),"motor_index");
		this.appendValueInput("motor_speed", Number)	
			.setCheck(Number)
			.appendField(Blockly.MOTOR_SPEED);
		this.setPreviousStatement(true);	
		this.setNextStatement(true);		
		this.setInputsInline(false);	
		this.setOutput(false);		
		
		this.setTooltip("运行笃行科技扩展板马达, 速度设置为负数则表示马达反转.");
	}	
}

Blockly.Blocks.get_speed = {
	init: function() {
		this.setColour(Blockly.Blocks.Rais_MixlyLib.Colour);
		
		this.appendDummyInput("")
			.appendField(Blockly.GET_MOTOR_SPEED);
		this.appendDummyInput("")
			.appendField(new Blockly.FieldDropdown(motor_array), "motor_index");
		
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setInputsInline(true);
		this.setOutput(true);
		
		this.setTooltip("获取笃行科技扩展板马达的速度, 值域为-255~255.");
	}
}

Blockly.Blocks.run_car = {
	init: function() {
		this.setColour(Blockly.Blocks.Rais_MixlyLib.Colour);
		
		this.appendDummyInput("")
			.appendField(Blockly.RUN_MOTOR_CAR);
		this.appendValueInput("motor_speed", Number)
			.setCheck(Number)
			.appendField(Blockly.SPEED);
		this.appendValueInput("motor_direction", Number)
			.setCheck(Number)
			.appendField(Blockly.DIRECTION);
			
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setInputsInline(false);
		this.setOutput(false);
		
		this.setTooltip("将笃行科技扩展板马达(M1和M2)的速度映射为速度和方向,\n其中方向为0表示前进;180表示后退;\n负数表示向左偏;正值表示向右偏.");
	}
}

Blockly.Blocks.set_wheels = {
	init: function() {
		this.setColour(Blockly.Blocks.Rais_MixlyLib.Colour);
		
		this.appendDummyInput("")
			.appendField(Blockly.SET_WHEELS)
			.appendField(new Blockly.FieldDropdown(wheels_array), "theLWheel");
		
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setInputsInline(true);
		this.setOutput(false);
		
		this.setTooltip("为小车轮子分配马达, 默认是: M1为左轮, M2为右轮.")
	}
}

Blockly.Blocks.set_rotation = {
	init: function() {
		this.setColour(Blockly.Blocks.Rais_MixlyLib.Colour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setInputsInline(true);
		this.setOutput(false);

		this.appendDummyInput("")
			.appendField(Blockly.SET)
			.appendField(new Blockly.FieldDropdown(wheel_LR_array), "theWheel")
			.appendField(new Blockly.FieldDropdown(wheel_rotation_array), "rotation")
			.appendField(Blockly.SET_ROTATION);
		
		this.setTooltip("分别设置小车轮子在前进时的旋转方向.")
	}
}


Blockly.Blocks.get_wheel_speed = {
	init: function() {
		this.setColour(Blockly.Blocks.Rais_MixlyLib.Colour);
		
		this.appendDummyInput("")
			.appendField(Blockly.GET_WHEEL_SPEED)
			.appendField(new Blockly.FieldDropdown(wheel_LR_array), "theWheel");
		
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setInputsInline(true);
		this.setOutput(true);
	
		this.setTooltip("获取轮子速度,值域为-255~255.");
	}
	
}

Blockly.Blocks.arg_pwr = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.Colour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setInputsInline(false);
		this.setOutput(true);
		// this.setTooltip();
		
		this.appendDummyInput("")
			.appendField('速度值');
	}
}

Blockly.Blocks.tip_sume_func_with_args = {
	init: function(){
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setInputsInline(false);
		this.setOutput(false);
		// this.setTooltip();
		
		this.appendDummyInput("")
			.appendField('这个函数(或这组操作)需要能够接收速度值');
		this.appendDummyInput("")		
			.appendField('并根据该速度值来控制马达速度.');
		this.appendValueInput("arg", Number)
			.appendField('(该图像块为提示, 使用时请移除.)');
	}
}

Blockly.Blocks.create_custom_wheels_map = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.Colour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setInputsInline(false);
		this.setOutput(false);
		// this.setTooltip();
		
		this.appendDummyInput("")
			.appendField('自定义映射');
		this.appendDummyInput("")
			.appendField('名称:')
			.appendField(new Blockly.FieldTextInput('my_car'), 'map_name');
		this.appendValueInput('max_speed', Number)
			.appendField('速度最大值:')
			.setCheck(Number);
		
		this.appendDummyInput("")
			.appendField('左轮运行方法:');
		this.appendStatementInput('run_left_wheel');
		this.appendDummyInput("")
			.appendField('右轮运行方法:');	
		this.appendStatementInput('run_right_wheel');
	}
}




//Rais_Deque -------------------------------------------------------------//

//unused: var mode_array = [[Blockly.USE_IDP, "true"],[Blockly.NOT_USE_IDP, "false"]];
var type_array = [[Blockly.INT, "int"],  	 [Blockly.LONG, "long"],	 [Blockly.FLOAT, "float"],
				  [Blockly.CHAR, "char"],    [Blockly.BYTE, "byte"],	 [Blockly.STRING, "String"],
				   [Blockly.DOUBLE, "double"], [Blockly.BOOL, "bool"], [Blockly.SHORT, "short"],[Blockly.CHAR_P, "char*"]];

var rp_array  = [["", ""], ["&", "&"], ["*", "*"]];

Blockly.Blocks.data_types = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LinkedListColour);
		this.setOutput(true, String);
		
		this.appendDummyInput("")
			.appendField(new Blockly.FieldDropdown(type_array), "type")
			.appendField(new Blockly.FieldDropdown(rp_array), "rp");
		
	}
}

Blockly.Blocks.call_methods = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LinkedListColour);
		this.setOutput(true, String);
		this.setTooltip(Blockly.TIP_CALL_METHODS);
		
		this.appendDummyInput("")
			.appendField(new Blockly.FieldTextInput(""), "dot_")
			.appendField(";");
	}
}

Blockly.Blocks.create_linkedlist = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LinkedListColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setInputsInline(true);
		this.setOutput(false);
		this.setTooltip(Blockly.TIP_CREATE_LINKEDLIST);


		this.appendDummyInput("")
			.appendField(Blockly.CREATE_LINKEDLIST);
		this.appendValueInput("data_type", String)
			.appendField(Blockly.DATA_TYPE + "<");
		
		this.appendDummyInput("")
			.appendField(">");
		this.appendDummyInput("")
			.appendField(Blockly.NEW_NAME)
			.appendField(new Blockly.FieldTextInput("list"), "list_name");
		// this.appendDummyInput("")
			// .appendField(Blockly.IDP_MODE)
			// .appendField(new Blockly.FieldCheckbox("TRUE"), "mode");
	
	}
}

Blockly.Blocks.user_defined_type = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LinkedListColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setInputsInline(true);
		this.setOutput(true, String);
		this.setTooltip(Blockly.TIP_USER_DEFINDED_TYPE);
		
		this.appendDummyInput("")
			.appendField(Blockly.USER_TYPE)
			.appendField(new Blockly.FieldTextInput("unsigned long"), "type_name");
		this.isInCreateBlock = false;
	},
	onchange: function(e){
		this.isInCreateBlock = false;
		var block = this;
		do{
			if(block.type == "create_linkedlist" ){
				this.isInCreateBlock = true;
				break;
			}
			block = block.getSurroundParent();
		}while(block);
		if(!this.isInCreateBlock)	this.setWarningText(Blockly.WARNING_USER_DEFINED_TYPE);
		else					this.setWarningText(null);
	}
}

Blockly.Blocks.get_length = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LinkedListColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setInputsInline(true);
		this.setOutput(true, Number);
		this.setTooltip(Blockly.TIP_GET_LENGTH);
		
		this.appendDummyInput("")
			.appendField(Blockly.GET_LIST)
			.appendField(new Blockly.FieldTextInput("list"), "list_name")
			.appendField(Blockly.LIST_LENGTH);
		
	}
}

Blockly.Blocks.get_r = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LinkedListColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_GET);
		
			
		this.appendValueInput("element_index", Number)
			.setCheck(Number)
			.appendField(Blockly.GET_LIST)
			.appendField(new Blockly.FieldTextInput("list"), "list_name")
			.appendField(Blockly.THE_);
			
		this.appendValueInput("dot", String)
			.setAlign(Blockly.RIGHT)
			.appendField(Blockly.INDEX_ELEMENT);
	
	}
}

Blockly.Blocks.get_l = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LinkedListColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_GET);
				
		this.appendValueInput("element_index", Number)
			.setCheck(Number)
			.appendField(Blockly.GET_LIST)
			.appendField(new Blockly.FieldTextInput("list"), "list_name")
			.appendField(Blockly.THE_);
			
		this.appendValueInput("dot", String)
			.setAlign(Blockly.RIGHT)
			.appendField(Blockly.INDEX_ELEMENT);
	
	}
}

Blockly.Blocks.push = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LinkedListColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setInputsInline(false);
		this.setOutput(false);
		this.setTooltip(Blockly.TIP_PUSH);
		
		this.appendValueInput("new_element", String)
			.appendField(Blockly.THE_LIST)
			.appendField(new Blockly.FieldTextInput("list"), "list_name")
			.appendField(Blockly.PUSH_DATA);
	}
}

Blockly.Blocks.unshift = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LinkedListColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setInputsInline(false);
		this.setOutput(false);
		this.setTooltip(Blockly.TIP_UNSHIFT);
		
		this.appendValueInput("new_element", String)
			.appendField(Blockly.THE_LIST)
			.appendField(new Blockly.FieldTextInput("list"), "list_name")
			.appendField(Blockly.UNSHIFT_DATA);
	}
}

Blockly.Blocks.add = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LinkedListColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setInputsInline(false);
		this.setOutput(false);
		this.setTooltip(Blockly.TIP_ADD);
		
		this.appendValueInput("new_element", String)
			.appendField(Blockly.THE_LIST)
			.appendField(new Blockly.FieldTextInput("list"), "list_name")
			.appendField(Blockly.ADD_DATA);
		this.appendValueInput("element_index", Number)
			.appendField(Blockly.TO_APPOINT_PLACE);
		
	}
}

Blockly.Blocks.shift_l = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LinkedListColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_SHIFT);
			
		this.appendValueInput("dot", String)
			.appendField(Blockly.GET_SHIFT)
			.appendField(new Blockly.FieldTextInput("list"), "list_name")
			.appendField(Blockly.LIST_HEAD_DATA)
			.setAlign(Blockly.ALIGN_RIGHT);
			
		
	}
}

Blockly.Blocks.shift_r = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LinkedListColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_SHIFT);
			
		this.appendValueInput("dot", String)
			.appendField(Blockly.GET_SHIFT)
			.appendField(new Blockly.FieldTextInput("list"), "list_name")
			.appendField(Blockly.LIST_HEAD_DATA)
			.setAlign(Blockly.ALIGN_RIGHT);
	}
}

Blockly.Blocks.pop_l = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LinkedListColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_POP);
			
		this.appendValueInput("dot", String)
			.appendField(Blockly.GET_SHIFT)
			.appendField(new Blockly.FieldTextInput("list"), "list_name")
			.appendField(Blockly.LIST_TAIL_DATA)
			.setAlign(Blockly.ALIGN_RIGHT);
			
		
	}
}

Blockly.Blocks.pop_r = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LinkedListColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_POP);
			
		this.appendValueInput("dot", String)
			.appendField(Blockly.GET_SHIFT)
			.appendField(new Blockly.FieldTextInput("list"), "list_name")
			.appendField(Blockly.LIST_TAIL_DATA)
			.setAlign(Blockly.ALIGN_RIGHT);	
			
	}
}

Blockly.Blocks.del_element = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LinkedListColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_DEL);
		
		this.appendValueInput("element_index", Number)
			.appendField(Blockly.THE_LIST)
			.appendField(new Blockly.FieldTextInput("list"), "list_name")
			.appendField(Blockly.DELETE_ELEMENT);
		this.appendDummyInput("")
			.appendField(Blockly.INDEX_ELEMENT);
	}
}

Blockly.Blocks.clear_list = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LinkedListColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_SHIFT_ALL);
		
		this.appendDummyInput("")
			.appendField(Blockly.THE_LIST)
			.appendField(new Blockly.FieldTextInput("list"), "list_name")
			.appendField(Blockly.SHIFT_ALL);
	}
}

//Rais_MatrixLED ---------------------------------------------------//
var point_states_array = [[Blockly.STATE_ON, "true"], [Blockly.STATE_OFF, "false"]];
var system_states_array = [[Blockly.SYS_DIS, "false"], [Blockly.SYS_ON, "true"]];
var settingMode = [[Blockly.NORMAL_MODE, 'normal_mode'], [Blockly.PROFESSIONAL_MODE, 'pro_mode']];

Blockly.Blocks.point_states = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.MatrixColour);
		this.setOutput(true);
		
		this.appendDummyInput("")
			.appendField(new Blockly.FieldDropdown(point_states_array), "point_state");
	}
}

Blockly.Blocks.create_matrixLED = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.MatrixColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_CREATE_MATRIX);
		
		this.appendDummyInput("")
			.appendField(Blockly.CREATE_MATRIX);
		this.appendDummyInput("")
			.appendField(Blockly.MAT_NAME)
			.appendField(new Blockly.FieldTextInput("matrix"), "mat_name");
		this.appendValueInput("cs_pin", Number)
			.setCheck(Number)
			.appendField(Blockly.CS);
		this.appendValueInput("wr_pin", Number)
			.setCheck(Number)
			.appendField(Blockly.WR);
		this.appendValueInput("data_pin", Number)
			.setCheck(Number)
			.appendField(Blockly.DATA);
	}
}

Blockly.Blocks.set_point_matrix = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.MatrixColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_SET_POINT);
		
		this.appendDummyInput("")
			.appendField(Blockly.THE_MATRIX)
			.appendField(new Blockly.FieldTextInput("matrix"), "mat_name");
		this.appendDummyInput("")
			.appendField(Blockly.SET_POINT);
			
		this.appendValueInput("p_x", Number)
			.setCheck(Number)
			.appendField(Blockly.POINT_X);
			
		this.appendValueInput("p_y", Number)
			.setCheck(Number)
			.appendField(Blockly.POINT_Y);

		this.appendValueInput("p_state", String);
		this.appendDummyInput("")
			.appendField(Blockly.PAINT_NOW)
			.appendField(new Blockly.FieldCheckbox("FALSE"), "paint_now");
			
	}
}

Blockly.Blocks.get_point_matrix = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.MatrixColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_GET_POINT);
		
		this.appendDummyInput("")
			.appendField(Blockly.THE_MATRIX)
			.appendField(new Blockly.FieldTextInput("matrix"), "mat_name");
		this.appendDummyInput("")
			.appendField(Blockly.GET_POINT);
		this.appendValueInput("p_x", Number)
			.setCheck(Number)
			.appendField(Blockly.POINT_X);
		this.appendValueInput("p_y", Number)
			.setCheck(Number)
			.appendField(Blockly.POINT_Y);
	
	
	}	
}
/*	removed.	

Blockly.Blocks.set_line_matrix = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.MatrixColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_SET_LINE);
		
		this.appendDummyInput("")
			.appendField(Blockly.THE_MATRIX)
			.appendField(new Blockly.FieldTextInput("matrix"), "mat_name");
		this.appendValueInput("line_y", Number)
			.setCheck(Number)
			.appendField(Blockly.SET_LINE_);
		this.appendValueInput("num", Number)
			.appendField(Blockly._LINE);
		this.appendDummyInput("")
			.appendField(Blockly.INPUTS_BIN)
			.appendField(new Blockly.FieldCheckbox("TRUE"), "inputs_bin");
		this.appendDummyInput("")
			.appendField(Blockly.PAINT_NOW)
			.appendField(new Blockly.FieldCheckbox("FALSE"), "paint_now");
	}
}
*/

Blockly.Blocks.fill_matrix = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.MatrixColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_FILL_RECT);
		
		this.appendDummyInput("")
			.appendField(Blockly.THE_MATRIX)
			.appendField(new Blockly.FieldTextInput("matrix"), "mat_name");
		this.appendValueInput("state", Number)
			.appendField(Blockly.FILL_MATRIX);
		this.appendValueInput("x", Number)
			.setCheck(Number)
			.appendField(Blockly.FROM_XY);
		this.appendValueInput("y", Number)
			.setCheck(Number);
		this.appendValueInput("end_x", Number)
			.setCheck(Number)
			.appendField(Blockly.TO_XY);
		this.appendValueInput("end_y", Number)
			.setCheck(Number);
		
		this.appendDummyInput("paint_now_field")
			.appendField(Blockly.PAINT_NOW)
			.appendField(new Blockly.FieldCheckbox("FALSE"), "paint_now");	
	}
}

Blockly.Blocks.set_string_matrix = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.MatrixColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_NORMAL_MODE_SET_STR);
			
		this.appendDummyInput("")
			.appendField(Blockly.THE_MATRIX)
			.appendField(new Blockly.FieldTextInput("matrix"), "mat_name");
		this.appendValueInput("str", String)
			.appendField(Blockly.SET_STRING)
			.setCheck(String);

		this.appendValueInput("shft_x", Number)
			.setCheck(Number)
			.appendField(Blockly.X);
		this.appendValueInput("shft_y", Number)
			.setCheck(Number)
			.appendField(Blockly.Y);
		
		this.appendDummyInput("paint_now_field")
			.appendField(Blockly.PAINT_NOW)
			.appendField(new Blockly.FieldCheckbox("FALSE"), "paint_now");	
	}
	
	//未完成
	/*	this.nowMode = 'normal_mode';
		this.settingMode = Blockly.PROFESSIONAL_MODE;
		this.paintState = "FALSE";
	},
	mutationToDom: function(){
		var container = document.createElement('mutation');
		container.setAttribute('is_normal_mode',  this.nowMode == 'normal_mode');
		return container;
	},
	domToMutation: function(xmlElement){
		this.nowMode = xmlElement.getAttribute('is_normal_mode') == 'true' ? 'normal_mode' : 'pro_mode';
		if(this.nowMode == 'pro_mode'){
			this.toggleMode('pro_mode');
		}
	},
	onchange: function(e){
		this.paintState = this.getFieldValue('paint_now');
	},
	customContextMenu: function(options){
		var option = {
			enabled: true,
			text: this.settingMode,
			callback: function() {
				if(this.settingMode == Blockly.PROFESSIONAL_MODE){
					this.settingMode = Blockly.NORMAL_MODE;
					this.toggleMode('pro_mode');
				}else {
					this.settingMode = Blockly.PROFESSIONAL_MODE;
					this.toggleMode('normal_mode');
				}
			}.bind(this)
		};
		options.push(option);
	},
	toggleMode: function(mode){	//mode's value:    'normal_mode'  or   'pro_mode'
		if(mode == this.nowMode) return;
		this.nowMode = mode;
		this.removeInput('shft_x');
		this.removeInput('shft_y');
		this.removeInput('paint_now_field');
		
		if(mode == 'normal_mode'){
			this.removeInput('x');
			this.removeInput('y');
			this.removeInput('end_x');
			this.removeInput('end_y');
			this.appendValueInput("shft_x", Number)
				.setCheck(Number)
				.appendField(Blockly.X);
			this.appendValueInput("shft_y", Number)
				.setCheck(Number)
				.appendField(Blockly.Y);
		}else {
			this.appendValueInput("x", Number)
				.setCheck(Number)
				.appendField(Blockly.FROM_XY);
			this.appendValueInput("y", Number)
				.setCheck(Number);
			this.appendValueInput("end_x", Number)
				.setCheck(Number)
				.appendField(Blockly.TO_XY);
			this.appendValueInput("end_y", Number)
				.setCheck(Number);
			this.appendValueInput("shft_x", Number)
				.setCheck(Number)
				.appendField(Blockly.SHFT_X);
			this.appendValueInput("shft_y", Number)
				.setCheck(Number)
				.appendField(Blockly.SHFT_Y);
		}
			this.appendDummyInput("paint_now_field")
				.appendField(Blockly.PAINT_NOW)
				.appendField(new Blockly.FieldCheckbox(this.paintState), "paint_now");	

	}*/
}

Blockly.Blocks.set_image_matrix = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.MatrixColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_NORMAL_MODE_SET_IMG);
		
		this.appendDummyInput("")
			.appendField(Blockly.THE_MATRIX)
			.appendField(new Blockly.FieldTextInput("matrix"), "mat_name");
		this.appendValueInput("img")
			.appendField(Blockly.SET_IMAGE);
		/*	
		this.appendValueInput("x", Number)
			.setCheck(Number)
			.appendField(Blockly.FROM_XY);
		this.appendValueInput("y", Number)
			.setCheck(Number);
		this.appendValueInput("end_x", Number)
			.setCheck(Number)
			.appendField(Blockly.TO_XY);
		this.appendValueInput("end_y", Number)
			.setCheck(Number);
		*/
		this.appendValueInput('shft_x', Number)
			.setCheck(Number)
			.appendField(Blockly.X)
		this.appendValueInput("shft_y", Number)
			.setCheck(Number)
			.appendField(Blockly.Y);
		this.appendDummyInput("")
			.appendField(Blockly.PAINT_NOW)
			.appendField(new Blockly.FieldCheckbox("FALSE"), "paint_now");
	}
}

Blockly.Blocks.paint_matrix = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.MatrixColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_PAINT_MAT);
		
		this.appendDummyInput("")
			.appendField(Blockly.PAINT_MATRIX)
			.appendField(new Blockly.FieldTextInput("matrix"), "mat_name");
	}
}

Blockly.Blocks.clear_matrix = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.MatrixColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_CLEAR_MAT);
		
		this.appendValueInput("state", Number)	
			.appendField(Blockly.THE_MATRIX)
			.appendField(new Blockly.FieldTextInput("matrix"), "mat_name")
			.appendField(Blockly.CLEAR_MATRIX);
		this.appendDummyInput("")
			.appendField(Blockly.CLEAR_NOW)
			.appendField(new Blockly.FieldCheckbox("TRUE"), "paint_now");
	}
}

Blockly.Blocks.blink_matrix = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.MatrixColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_BLINK_MAT);
		
		this.appendDummyInput("")
			.appendField(Blockly.THE_MATRIX)
			.appendField(new Blockly.FieldTextInput("matrix"), "mat_name")
			.appendField(Blockly.BLINK_MATRIX)
			.appendField(new Blockly.FieldDropdown([[Blockly.BLINK_ON, "true"], [Blockly.BLINK_OFF, "false"]]), "blink_state");
	}
}

Blockly.Blocks.set_brightness_matrix = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.MatrixColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_SET_BITNS);
		
		this.appendValueInput("brightness", Number)
			.appendField(Blockly.THE_MATRIX)
			.appendField(new Blockly.FieldTextInput("matrix"), "mat_name")
			.setCheck(Number)
			.appendField(Blockly.SET_BRIGHTNESS);
	}
}

Blockly.Blocks.set_system = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.MatrixColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_SET_SYSTEM);
		
		this.appendDummyInput("")
			.appendField(Blockly.THE_MATRIX)
			.appendField(new Blockly.FieldTextInput("matrix"), "mat_name")
			.appendField(new Blockly.FieldDropdown(system_states_array),  "system_state");
	}
}

Blockly.Blocks.create_polyMatrixLED = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.MatrixColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_CREATE_POLY_MAT);
		
		this.appendDummyInput("")
			.appendField(Blockly.CREATE_POLY_MAT);
		this.appendDummyInput("")
			.appendField(Blockly.POLY_MAT_NUM + "<")
			.appendField(new Blockly.FieldTextInput("4"), "mat_num")
			.appendField(">");
		this.appendDummyInput("")
			.appendField(Blockly.MAT_NAME)
			.appendField(new Blockly.FieldTextInput("matrix"), "mat_name");
		this.appendValueInput("cs_pin", Number)
			.setCheck(Number)
			.appendField(Blockly.CS);
		this.appendValueInput("wr_pin", Number)
			.setCheck(Number)
			.appendField(Blockly.WR);
		this.appendDummyInput("")
			.appendField(Blockly.DATAS)
			.appendField(new Blockly.FieldTextInput("0, 0, 0, 0"), "mat_data_pins");
		
		//DATAS field check
		this.hasCNComma = false;
		this.hasTailComma = false;
		this.hasRepeatComma = false;
	},
	onchange: function(e){
		if(this.hasCNComma) 			this.setWarningText(Blockly.WARNING_DEQUE_CN_COMMA);
		else {
			if(this.hasTailComma)		this.setWarningText(Blockly.WARNING_DEQUE_TAIL_COMMA);
			else	{
				if(this.hasRepeatComma)this.setWarningText(Blockly.WARNING_DEQUE_REPEAT_COMMA);
				else {
					this.setWarningText(null);
				}
			}
		}
	}
}


//Rais_Graph -------------------------------------------------------------//

var orthogonal_dir_arr = [[Blockly.O_NORTH, "rais::O_NORTH"],[Blockly.O_EAST, "rais::O_EAST"],[Blockly.O_SOUTH, "rais::O_SOUTH"],[Blockly.O_WEST, "rais::O_WEST"]];
var orthogonal_turning_arr = [[Blockly.O_GO_AHEAD, "rais::O_GO_STRAIGHT"],[Blockly.O_TURN_RIGHT, "rais::O_TURN_RIGHT"],[Blockly.O_GO_BACK, "rais::O_TURN_AROUND"],[Blockly.O_TURN_LEFT, "rais::O_TURN_LEFT"]];

var links_direction_arr = [[Blockly.FROM_S_TO_N, "rais::O_NORTH"], [Blockly.FROM_W_TO_E, "rais::O_EAST"], [Blockly.FROM_N_TO_S, "rais::O_SOUTH"], [Blockly.FROM_E_TO_W, "rais::O_WEST"] ];

Blockly.Blocks.orthogonal_dir = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setTooltip(Blockly.TIP_O_DIR);
		
		this.appendDummyInput("")
			.appendField(new Blockly.FieldDropdown(orthogonal_dir_arr), "dir")
			.appendField(Blockly.O_ONLY);
	}
}

Blockly.Blocks.orthogonal_dir_no_tip = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setTooltip(Blockly.TIP_O_DIR);
		
		this.appendDummyInput("")
			.appendField(new Blockly.FieldDropdown(orthogonal_dir_arr), "dir");
	}
}

Blockly.Blocks.orthogonal_turning = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setTooltip(Blockly.TIP_O_TURNING);
		
		this.appendDummyInput("")
			.appendField(new Blockly.FieldDropdown(orthogonal_turning_arr), "dir")
			.appendField(Blockly.O_ONLY);
	}
}


Blockly.Blocks.create_graph = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(false);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_CREATE_GRAPH);
		
		this.appendDummyInput("")
			.appendField(Blockly.CREATE_GRAPH);
		this.appendDummyInput("")
			.appendField(Blockly.INIT_PF);
		this.appendDummyInput("")
			.appendField(Blockly.GRAPH_NAME)
			.appendField(new Blockly.FieldTextInput("graph"), "graph_name");
		this.appendValueInput("vertexNum", Number)
			.appendField(Blockly.VERTEX_NUM)
			.setCheck(Number);
		this.appendValueInput("maxEdgeNum", Number)
			.appendField(Blockly.MAX_EDGE_NUM)
			.setCheck(Number);
		this.appendStatementInput("build")
			.appendField(Blockly.GRAPH_BUILD);
	}
}

Blockly.Blocks.create_orthogonal_graph = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(false);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(false);	
		this.setTooltip(Blockly.TIP_CREATE_OGRAPH);
		
		this.appendDummyInput("")
			.appendField(Blockly.CREATE_ORTHOGONAL_GRAPH);
		this.appendDummyInput("")
			.appendField(Blockly.INIT_PF);
		this.appendDummyInput("")
			.appendField(Blockly.GRAPH_NAME)
			.appendField(new Blockly.FieldTextInput("graph"), "graph_name");
		this.appendValueInput("vertexNum", Number)
			.appendField(Blockly.VERTEX_NUM)
			.setCheck(Number);
		this.appendStatementInput("build")
			.appendField(Blockly.GRAPH_BUILD);
	}
}

Blockly.Blocks.graph_link = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(true);	
		this.setTooltip(Blockly.TIP_LINK);
		
		this.appendDummyInput("inp_name")
			.appendField(Blockly.NORMAL_GRAPH)
			.appendField(new Blockly.FieldTextInput("graph"), "graph_name");
		this.appendValueInput("host_idx", Number)
			.appendField(Blockly.THE_VERTEX)
			.setCheck(Number);
		this.appendValueInput("host_edge_idx", Number)
			.appendField(Blockly.EDGE)
			.setCheck(Number);
		this.appendDummyInput("")
			.appendField(Blockly.GRAPH_LINK);
		this.appendValueInput("adj_idx", Number)
			.appendField(Blockly.THE_VERTEX)
			.setCheck(Number);
		this.appendValueInput("adj_edge_idx", Number)
			.appendField(Blockly.EDGE)
			.setCheck(Number);
		
		//Abandoned:
		this.isInBuild = false;
	},
	onchange: function(e){
		var block = this;
		do{
			if(block.type == "create_graph" &&
				block.getFieldValue("graph_name") == this.getFieldValue("graph_name") ){
				this.isInBuild = true;
				return;
			}
			block = block.getSurroundParent();
		}while(block);
		this.isInBuild = false;
	}
}

Blockly.Blocks.orthogonal_graph_link = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(true);	
		this.setTooltip(Blockly.TIP_OLINK);

		this.appendDummyInput("")
			.appendField(Blockly.ORTHOGONAL_GRAPH)
			.appendField(new Blockly.FieldTextInput("graph"), "graph_name");
		this.appendValueInput("host_idx", Number)
			.appendField(Blockly.THE_VERTEX)
			.setCheck(Number);
		this.appendValueInput("host_edge_idx", Number)
			.appendField(Blockly.DOT_S)
			.setCheck(Number);
		this.appendValueInput("adj_idx", Number)
			.appendField(Blockly.GRAPH_LINK)
			.appendField(Blockly.THE_VERTEX)
			.setCheck(Number);
		
		//Abandoned:
		this.isInBuild = false;
	},
	onchange: function(e){
		var block = this;
		do{
			if(block.type == "create_orthogonal_graph" || block.type == "builtin_graph_logistics"  &&
				block.getFieldValue("graph_name") == this.getFieldValue("graph_name") ){
				this.isInBuild = true;
				return;
			}
			block = block.getSurroundParent();
		}while(block);
		this.isInBuild = false;
	}
}

Blockly.Blocks.orthogonal_graph_links = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(true);	
		this.setTooltip(Blockly.TIP_OLINKLN);
		
		this.appendDummyInput("")
			.appendField(Blockly.ORTHOGONAL_GRAPH)
			.appendField(new Blockly.FieldTextInput("graph"), "graph_name");
		this.appendDummyInput("")
			.appendField(Blockly.LINK_LINE)
			.appendField(new Blockly.FieldDropdown(links_direction_arr), "links_direction_arr");
		this.appendDummyInput("")
			.appendField(Blockly.THE_VERTEX)
			.appendField(new Blockly.FieldTextInput("1, 2, 3, 4"), 'Vs');
		
		//Abandoned:
		this.isInBuild = false;
		
		this.hasCNComma = false;
		this.hasTailComma = false;
		this.hasRepeatComma = false;
	},
	
	onchange: function(e){
		this.isInBuild = false;
		var block = this;
		do{
			if(block.type == "create_orthogonal_graph" || block.type == "builtin_graph_logistics" &&
				block.getFieldValue("graph_name") == this.getFieldValue("graph_name") ){
				this.isInBuild = true;
				break;
			}
			block = block.getSurroundParent();
		}while(block);
		
		if(this.hasCNComma){this.setWarningText(Blockly.WARNING_OLINKLN_HAS_CN_COMMA); return;}
		if(this.hasTailComma){this.setWarningText(Blockly.WARNING_OLINKLN_HAS_TAIL_COMMA); return;}
		if(this.hasRepeatComma){this.setWarningText(Blockly.WARNING_OLINKLN_HAS_REPEAT_COMMA); return;}
		this.setWarningText(null);
	}
}

Blockly.Blocks.graph_cut = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(true);	
		this.setTooltip(Blockly.TIP_CUT);
		
		this.appendDummyInput("")
			.appendField(new Blockly.FieldTextInput("graph"), "graph_name");
			
		this.appendValueInput("host_idx", Number)
			.appendField(Blockly.THE_VERTEX)
			.setCheck(Number);
			
		this.appendValueInput("host_edge_idx", Number)
			.appendField(Blockly.CUT)
			.setCheck(Number);
		
		this.isInBuild = false;
	},
	onchange: function(e){
		var block = this;
		this.isInBuild = false;
		do{
			if((block.type == "create_orthogonal_graph" || block.type == "create_graph") &&
				block.getFieldValue("graph_name") == this.getFieldValue("graph_name") ){
				this.isInBuild = true;
				return;
			}
			block = block.getSurroundParent();
		}while(block);
	}
}

Blockly.Blocks.graph_get_edge_idx = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(true);	
		this.setTooltip(Blockly.TIP_GET_EDGE_IDX);
		
		this.appendDummyInput("")
			.appendField(new Blockly.FieldTextInput("graph"), "graph_name");
		
		this.appendValueInput("host_idx", Number)
			.appendField(Blockly.FROM_VERTEX)
			.setCheck(Number);
		
		this.appendValueInput("adj_idx", Number)
			.appendField(Blockly.TO_VERTEX)
			.setCheck(Number);
			
		this.appendDummyInput("")
			.appendField(Blockly.THE_EDGE_INDEX);
			
		this.isInBuild = false;
	},
	onchange: function(e){
		var block = this;
		do{
			if((block.type == "create_orthogonal_graph" || block.type == "create_graph") &&
				block.getFieldValue("graph_name") == this.getFieldValue("graph_name") ){
				this.isInBuild = true;
				return;
			}
			block = block.getSurroundParent();
		}while(block);
		this.isInBuild = false;
	}
}

var start_target_arr = [[Blockly.GRAPH_START, "Start"], [Blockly.GRAPH_DEST, "Dest"]];
var pf_result_constant_arr = [[Blockly.PF_FOUND, "rais::FOUND"], [Blockly.PF_HASN_FOUND, "rais::HASN_FOUND"], [Blockly.PF_UNREACHABLE_DEST, "rais::UNREACHABLE_DEST"], [Blockly.PF_NULL_GRAPH, "rais::NULL_GRAPH"], [Blockly.PF_NULL_START, "rais::NULL_START"], [Blockly.PF_NULL_DEST, "rais::NULL_DEST"]];

Blockly.Blocks.pf_set = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(false);	
		this.setTooltip(Blockly.TIP_GRAPH_SET);
		
		this.appendValueInput("idx", Number)
			.appendField(new Blockly.FieldTextInput("graph"), "graph_name")
			.appendField(Blockly.GRAPH_SET)
			.appendField(new Blockly.FieldDropdown(start_target_arr), "s_t" )
			.setCheck(Number);
	},
	onchange: function(e){
		var block = this;
		var legal = true;
		do{
			if((block.type == "create_orthogonal_graph" || block.type == "create_graph") &&
				block.getFieldValue("graph_name") == this.getFieldValue("graph_name") ){
				legal = false;
				break;
			}
			block = block.getSurroundParent();
		}while(block);
		if(legal)	this.setWarningText(null);
		else 	this.setWarningText(Blockly.WARNING_PF_IN_BUILD);
	}
}

Blockly.Blocks.pf_get = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(true);	
		this.setTooltip(Blockly.TIP_GRAPH_GET);
		
		this.appendDummyInput("")
			.appendField(new Blockly.FieldTextInput("graph"), "graph_name")
			.appendField(Blockly.GRAPH_GET)
			.appendField(new Blockly.FieldDropdown(start_target_arr), "s_t");
	},
	onchange: function(e){
		var block = this;
		var legal = true;
		do{
			if((block.type == "create_orthogonal_graph" || block.type == "create_graph") &&
				block.getFieldValue("graph_name") == this.getFieldValue("graph_name") ){
				legal = false;
				break;
			}
			block = block.getSurroundParent();
		}while(block);
		if(legal)	this.setWarningText(null);
		else 	this.setWarningText(Blockly.WARNING_PF_IN_BUILD);
	}
}

Blockly.Blocks.pf_find = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(true);	
		this.setTooltip(Blockly.TIP_FINDING);
			
		this.appendDummyInput("")
			.appendField(new Blockly.FieldTextInput("graph"), "graph_name")
			.appendField(Blockly.FINDING);
	},
	onchange: function(e){
		var block = this;
		var legal = true;
		do{
			if((block.type == "create_orthogonal_graph" || block.type == "create_graph") &&
				block.getFieldValue("graph_name") == this.getFieldValue("graph_name") ){
				legal = false;
				break;
			}
			block = block.getSurroundParent();
		}while(block);
		if(legal)	this.setWarningText(null);
		else 		this.setWarningText(Blockly.WARNING_PF_IN_BUILD);
	}
}

Blockly.Blocks.pf_finding_result = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(true);	
		this.setTooltip(Blockly.TIP_PF_FINDING_RESULT);
		
		this.appendDummyInput("")
			.appendField(new Blockly.FieldTextInput("graph"), "graph_name")
			.appendField(Blockly.FINDING_RESULT);
	
	},
	onchange: function(e){
		var block = this;
		var legal = true;
		do{
			if((block.type == "create_orthogonal_graph" || block.type == "create_graph") &&
				block.getFieldValue("graph_name") == this.getFieldValue("graph_name") ){
				legal = false;
				break;
			}
			block = block.getSurroundParent();
		}while(block);
		if(legal)	this.setWarningText(null);
		else 		this.setWarningText(Blockly.WARNING_PF_IN_BUILD);
	}
}

Blockly.Blocks.pf_finding_result_constant = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(true);	
		// this.setTooltip(Blockly.);
		
		this.appendDummyInput("")
			.appendField(Blockly.FINDING_RESULT + ":")
			.appendField(new Blockly.FieldDropdown(pf_result_constant_arr), "result_constant");
			
		
	}
}

Blockly.Blocks.pf_path_length = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(true);	
		this.setTooltip(Blockly.TIP_PF_LENGTH);
		
		this.appendDummyInput("")
			.appendField(new Blockly.FieldTextInput("graph"), "graph_name")
			.appendField(Blockly.GET_PATH_LENGTH);
	},
	onchange: function(e){
		var block = this;
		var legal = true;
		do{
			if((block.type == "create_orthogonal_graph" || block.type == "create_graph") &&
				block.getFieldValue("graph_name") == this.getFieldValue("graph_name") ){
				legal = false;
				break;
			}
			block = block.getSurroundParent();
		}while(block);
		if(legal)	this.setWarningText(null);
		else 		this.setWarningText(Blockly.WARNING_PF_IN_BUILD);
	}
}

Blockly.Blocks.pf_path_get = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(true);	
		this.setTooltip(Blockly.TIP_PF_GET);
			
		this.appendDummyInput("")
			.appendField(new Blockly.FieldTextInput("graph"), "graph_name");
		
		this.appendValueInput("path_idx", Number)
			.appendField(Blockly.GET_PATH_THE_)
			.setCheck(Number);
		this.appendDummyInput("")
			.appendField(Blockly.GET_PATH_VERTEX);
				
	},
	onchange: function(e){
		var block = this;
		var legal = true;
		do{
			if((block.type == "create_orthogonal_graph" || block.type == "create_graph") &&
				block.getFieldValue("graph_name") == this.getFieldValue("graph_name") ){
				legal = false;
				break;
			}
			block = block.getSurroundParent();
		}while(block);
		if(legal)	this.setWarningText(null);
		else 		this.setWarningText(Blockly.WARNING_PF_IN_BUILD);
	}
}

Blockly.Blocks.builtin_graph_logistics = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(false);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(false);	
		this.setTooltip(Blockly.TIP_BUINTIN_GRAPH_LOGISTICS);
		this.setCommentText(Blockly.COMMENT_BUILTIN_GRAPH_LOGISTICS);
		
		this.appendDummyInput("")
			.appendField(Blockly.SET_BUILTIN_GRAPH_LOGISTICS)
			.appendField(new Blockly.FieldTextInput("graph"), "graph_name");
		this.appendDummyInput("")
			.appendField(Blockly.GRAPH_BUILD + "(请勿尝试更改或移除, 否则可能导致无法预料的异常.)");
		this.appendStatementInput("builds");
			
		this.oldName = "";
	},
	onchange: function(e){
		this.oldName = this.getFieldValue("graph_name");
		var block = this.getChildren(true)[0];
		while(block){
			if(block.getFieldValue("graph_name") != null) {
				block.isInBuild = true;
				block.setFieldValue(this.oldName, "graph_name");
			}
			block = block.getNextBlock();
		}
	}
}

Blockly.Blocks.get_turning_dir = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(false);	
		this.setTooltip(Blockly.TIP_GET_TURNING_DIR);
		
		this.appendDummyInput("")
			.appendField(Blockly.ORTHOGONAL_GRAPH + ':' + Blockly.GET_TURNING_DIR);
		this.appendValueInput("fasing", Number)
			.appendField(Blockly.NOW_FASING)
			.setCheck(Number);
		this.appendValueInput("abs_turning_dir", Number)
			.appendField(Blockly.NEXT_DIRECTION)
			.setCheck(Number);
	}
}

Blockly.Blocks.get_o_turningbehavior = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.GraphColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(false);	
		this.setTooltip(Blockly.TIP_GETTURNING_BEHAVIOR);
		
		this.appendDummyInput("")
			.appendField(Blockly.ORTHOGONAL_GRAPH)
			.appendField(new Blockly.FieldTextInput("graph"), "graph_name");
		this.appendDummyInput("")
			.appendField(Blockly.GET_TURNING_DIR);
		
		this.appendValueInput("o_last_vertex", Number)
			.appendField(Blockly.O_LAST_VERTEX);
		this.appendValueInput("o_current_vertex", Number)
			.appendField(Blockly.O_CURRENT_VERTEX);
		this.appendValueInput("o_next_vertex", Number)
			.appendField(Blockly.O_NEXT_VERTEX);
	}
}

//Rais_LineTracker:	-----------------------------------------------------------//

Blockly.Blocks.motion_pwr = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LineTrackerColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_MOTION_PWR);
		this.appendDummyInput("")
			.appendField(Blockly.MOTION_PWR);
	},
	onchange: function(e){
		var block = this;
		var legal = false;
		do{
			if(block.type == "create_line_tracker" || block.type == "create_traveller"){
				legal = true;
				break;
			}
			block = block.getSurroundParent();
		}while(block);
		if(legal)	this.setWarningText(null);
		else 		this.setWarningText(Blockly.WARNING_MOTION);
	}
	
}
Blockly.Blocks.motion_dir = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LineTrackerColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_MOTION_DIR);
		this.appendDummyInput("")
			.appendField(Blockly.MOTION_DIR);
	},
	onchange: function(e){
		var block = this;
		var legal = false;
		do{
			if(block.type == "create_line_tracker" || block.type == "create_traveller"){
				legal = true;
				break;
			}
			block = block.getSurroundParent();
		}while(block);
		if(legal)	this.setWarningText(null);
		else 	this.setWarningText(Blockly.WARNING_MOTION);
	}
}

var return_state_arr = [[Blockly.IS_TRUE, "true"] , [Blockly.IS_FALSE, "false"]];

Blockly.Blocks.is_state_return_switch = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LineTrackerColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(false);
		// this.setTooltip();
		
		this.appendDummyInput("")
			.appendField(new Blockly.FieldDropdown(return_state_arr), "state");
	}
}

Blockly.Blocks.is_state_return = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LineTrackerColour);
		this.setPreviousStatement(true);
		this.setNextStatement(false);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_IS_STATE_RETURN);
		
		this.appendValueInput("isState", Boolean)
			.appendField(Blockly.IS_STATE_RETURN);
	},
	onchange: function(e){
		var block = this;
		var legal = false;
		do{
			if(block.type == "create_line_tracker" || block.type == "create_traveller" || block.type == "line_tracker_override_is_state_fun"){
				legal = true;
				break;
			}
			block = block.getSurroundParent();
		}while(block);
		if(legal)	this.setWarningText(null);
		else 		this.setWarningText(Blockly.WARNING_IS_STATE);
	}
}

var is_vertex_state_arr = [[Blockly.TRAVELLER_LEFT_V, "true"], [Blockly.TRAVELLER_NOT_LEFT_V, "false"] ];

Blockly.Blocks.if_left_vertex_switch = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LineTrackerColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(true);
		// this.setTooltip();
		
		this.appendDummyInput("")
			.appendField(new Blockly.FieldDropdown(is_vertex_state_arr) , "isState");
	}
}

Blockly.Blocks.if_left_vertex = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LineTrackerColour);
		this.setPreviousStatement(true);
		this.setNextStatement(false);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_IF_LEFT_VERTEX);
		
		this.appendValueInput("isState", Boolean)
			.appendField(Blockly.TRAVELLER_RETURN);
	},
	onchange: function(e){
		var block = this;
		var legal = false;
		do{
			if(block.type == "traveller_addTurningFunction"){
				legal = true;
				break;
			}
			block = block.getSurroundParent();
		}while(block);
		if(legal)	this.setWarningText(null);
		else 	this.setWarningText(Blockly.WARNING_ADD_TURNING_FUNC);
	}
}

Blockly.Blocks.create_line_tracker = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LineTrackerColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_CREATE_LINE_TRACKER);
		
		this.appendDummyInput("")
			.appendField(Blockly.CREATE_LINETRACKER);
		this.appendDummyInput("")
			.appendField(Blockly.LINETRACKER_NAME)
			.appendField(new Blockly.FieldTextInput("tracker"), "name");
		this.appendDummyInput("")
			.appendField(Blockly.SENS_NUM + "<")
			.appendField(new Blockly.FieldTextInput("5"), "sens_num")
			.appendField(">");
		this.appendDummyInput("")
			.appendField(Blockly.SENS_PINS)
			.appendField(new Blockly.FieldTextInput("0, 0, 0, 0, 0"), "pins");
		this.appendValueInput("pins_black_state", Number)
			.appendField(Blockly.TIGGED_BLACK);
		
		this.appendDummyInput("").appendField(Blockly.MOTION);
		this.appendStatementInput("motion");
		this.appendDummyInput("").appendField(Blockly.IS_NORMAL_STATE);
		this.appendStatementInput("isNormalState");
	
		this.hasCNComma = false;
		this.hasTailComma = false;
		this.hasRepeatComma = false;
	},
	onchange: function(e){
		if(this.hasCNComma){this.setWarningText(Blockly.WARNING_LT_CN_COMMA); return;}
		if(this.hasTailComma){this.setWarningText(Blockly.WARNING_LT_TAIL_COMMA); return;}
		if(this.hasRepeatComma){this.setWarningText(Blockly.WARNING_LT_REPEAT_COMMA); return;}
		this.setWarningText(null);
	}
}


Blockly.Blocks.create_traveller = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LineTrackerColour);
		this.setPreviousStatement(true);
		this.setNextStatement(false);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_CREATE_TRAVELLER);
		
		this.appendDummyInput("")
			.appendField(Blockly.CREATE_TRAVELLER);
		this.appendDummyInput("")
			.appendField(Blockly.LINETRACKER_NAME)
			.appendField(new Blockly.FieldTextInput("tracker"), "name");
		this.appendDummyInput("")
			.appendField(Blockly.SENS_NUM + "<")
			.appendField(new Blockly.FieldTextInput("5"), "sens_num")
			.appendField(">");
		this.appendDummyInput("")
			.appendField(Blockly.SENS_PINS)
			.appendField(new Blockly.FieldTextInput("0, 0, 0, 0, 0"), "pins");
		this.appendDummyInput("")
			.appendField(Blockly.GUIDE)
			.appendField(new Blockly.FieldTextInput("graph"), "graph_name");
		this.appendValueInput("pins_black_state", Number)
			.appendField(Blockly.TIGGED_BLACK);
		this.appendValueInput("fasing", Number)
			.appendField(Blockly.FASING);
		
		this.appendDummyInput("").appendField(Blockly.MOTION);
		this.appendStatementInput("motion");
		this.appendDummyInput("").appendField(Blockly.IS_NORMAL_STATE);
		this.appendStatementInput("isNormalState");
		this.appendDummyInput("").appendField(Blockly.IS_REACHING_VERTEX_STATE);
		this.appendStatementInput("isReachingVertexState");
		
		this.hasCNComma = false;
		this.hasTailComma = false;
		this.hasRepeatComma = false;
	},
	onchange: function(e){
		if(this.hasCNComma){this.setWarningText(Blockly.WARNING_LT_CN_COMMA); return;}
		if(this.hasTailComma){this.setWarningText(Blockly.WARNING_LT_TAIL_COMMA); return;}
		if(this.hasRepeatComma){this.setWarningText(Blockly.WARNING_LT_REPEAT_COMMA); return;}
		this.setWarningText(null);
	}
}

Blockly.Blocks.line_tracker_run = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LineTrackerColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_TRACKER_RUN);
		
		this.appendDummyInput("")
			.appendField(new Blockly.FieldTextInput("tracker"), "name")
			.appendField(Blockly.LINE_TRACKER_RUN);
		
		this.isInCls = false;
	},
	onchange: function(e){
		var block = this;
		this.isInCls = false;
		do{
			if(block.type == "create_line_tracker" ||
				block.type == "create_traveller"||
				block.type == "line_tracker_override_is_state_fun" ||
				block.type == "line_tracker_override_state_fun" )
				if(block.getFieldValue("name") ==  this.getFieldValue("name")){
					this.isInCls = true;
					return;
				}
			block = block.getSurroundParent();
		}while(block);
	}
}

var lineTracker_sets_arr = [[Blockly.LINE_TRACKER_POWER, "setPower"], 
					 [Blockly.LINE_TRACKER_CORRECTING_DEG, "setCorrectingDegree"], 
					 [Blockly.LINE_TRACKER_CENTER_LINE, "centerLine"], 
					 [Blockly.LINE_TRACKER_IS_RUNNING, "isRunning"] ];

var lineTracker_gets_arr = [[Blockly.LT_POWER, "getPower()"], 
					  [Blockly.LT_CORRECTING_DEG, "getCorrectingDegree()"],
					  [Blockly.LT_CENTER_LINE, "centerLine"],
					  [Blockly.LT_IS_RUNNING, "isRunning"],
					  [Blockly.LT_TRIGGED_NUM, "triggedNum"],
					  [Blockly.LT_TENDENCY, "tendency"],
					  [Blockly.LT_IS_CONSECUTIVE, "isConsecutive"],
					  [Blockly.LT_IS_NORMAL, "isNormalState()"],
					  [Blockly.LTT_FASING, "fasing"],	
					  [Blockly.LTT_IS_REACHING_VERTEX, "isReachingVertexState()"], 
					  [Blockly.LTT_TASK_NUM, "getTaskNum()"]];
					

Blockly.Blocks.line_tracker_set = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LineTrackerColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_TRACKER__SET);
		
		this.appendValueInput("value", Number)
			.appendField(new Blockly.FieldTextInput("tracker"), "name")
			.appendField(Blockly.LINE_TRACKER_SET)
			.appendField(new Blockly.FieldDropdown(lineTracker_sets_arr), "sets");
		this.isInCls = false;
	},
	onchange: function(e){
		var block = this;
		this.isInCls = false;
		do{
			if(block.type == "create_line_tracker" || 
				block.type == "create_traveller" ||
				block.type == "line_tracker_override_is_state_fun" ||
				block.type == "line_tracker_override_state_fun" )
				if(block.getFieldValue("name") ==  this.getFieldValue("name")){
					this.isInCls = true;
					return;
				}
			block = block.getSurroundParent();
		}while(block);
	}
}

Blockly.Blocks.line_tracker_get = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LineTrackerColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_TRACKER_GET);
		
		this.appendDummyInput("")
			.appendField(new Blockly.FieldTextInput("tracker"), "name")
			.appendField(Blockly.LT_GET)
			.appendField(new Blockly.FieldDropdown(lineTracker_gets_arr), "gets");
		this.isInCls = false;
	},
	onchange: function(e){
		var block = this;
		this.isInCls = false;
		do{
			if(block.type == "create_line_tracker" ||
				block.type == "create_traveller" ||
				block.type == "line_tracker_override_is_state_fun" ||
				block.type == "line_tracker_override_state_fun" )
				if(block.getFieldValue("name") ==  this.getFieldValue("name")){
					this.isInCls = true;
					return;
				}
			block = block.getSurroundParent();
		}while(block);
	}
}

Blockly.Blocks.line_tracker_get_sens = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LineTrackerColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_TRACKER_GET_SENS);
		
		this.appendValueInput("sens_idx", Number)
			.appendField(new Blockly.FieldTextInput("tracker"), "name")
			.appendField(Blockly.LT_GET)
			.setCheck(Number);
		this.appendDummyInput("")
			.appendField(Blockly.LT_SEN);
		this.isInCls = false;
	},
	onchange: function(e){
		var block = this;
		this.isInCls = false;
		do{
			if(block.type == "create_line_tracker" ||
				block.type == "create_traveller" ||
				block.type == "line_tracker_override_is_state_fun" ||
				block.type == "line_tracker_override_state_fun" )
				if(block.getFieldValue("name") ==  this.getFieldValue("name")){
					this.isInCls = true;
					return;
				}
			block = block.getSurroundParent();
		}while(block);
	}
}

var override_state_funs_arr = [   [Blockly.LT_NORMAL, "normalState"],
						   [Blockly.LT_CORRECTING, "correctingState"],
						   [Blockly.LT_INERTIA, "inertiaState"],
						   [Blockly.LT_EXCEPTION, "exceptionState"],
						   [Blockly.LT_END, "endState"]];

var override_is_state_funs_arr = [[Blockly.LT_CORRECTING, "isCorrectingState"],
						   [Blockly.LT_INERTIA, "isInertiaState"]];

Blockly.Blocks.line_tracker_override_is_state_fun = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LineTrackerColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(false);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_TRACKER_OVERRIDE_IS_STATE_FUN);
		
		this.appendDummyInput("")
			.appendField(new Blockly.FieldTextInput("tracker"), "name")
			.appendField(Blockly.LT_OVERRIDE_IS_STATE)
			.appendField(new  Blockly.FieldDropdown(override_is_state_funs_arr), "fun");
		this.appendStatementInput("codes");
	}
}

Blockly.Blocks.line_tracker_override_state_fun = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LineTrackerColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(false);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_TRACKER_OVERRIDE_STATE_FUN);
		
		this.appendDummyInput("")
			.appendField(new Blockly.FieldTextInput("tracker"), "name")
			.appendField(Blockly.LT_OVERRIDE_STATE)
			.appendField(new  Blockly.FieldDropdown(override_state_funs_arr), "fun");
		this.appendStatementInput("codes");
		
	}
}

Blockly.Blocks.traveller_addTurningFunction = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LineTrackerColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(false);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_TRACKER_ADD_TURNING_FUNC);
		
		this.appendValueInput("direction", Number)
			.appendField(new Blockly.FieldTextInput("tracker"), "name")
			.appendField(Blockly.TRAVELLER_ADD_TURNING_FUNC)
			.setCheck(Number);
		
		this.appendStatementInput("codes");
	}
}

Blockly.Blocks.traveller_addTask = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.LineTrackerColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_TRAVELLER_ADD_TASK);
		
		this.appendValueInput("vertex_idx", Number)
			.appendField(new Blockly.FieldTextInput("tracker"), "name")
			.appendField(Blockly.TRAVELLER_ADD_TASK)
			.setCheck(Number);
		this.isInCls = false;
	},
	onchange: function(e){
		var block = this;
		this.isInCls = false;
		do{
			if(block.type == "create_line_tracker" ||
				block.type == "create_traveller" ||
				block.type == "line_tracker_override_is_state_fun" ||
				block.type == "line_tracker_override_state_fun" )
				if(block.getFieldValue("name") ==  this.getFieldValue("name")){
					this.isInCls = true;
					return;
				}
			block = block.getSurroundParent();
		}while(block);
	}
}


//Rais_Utils---------------------------------------------------------------------//
Blockly.Blocks.in_range = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.UtilsColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_IN_RANGE);
		
		this.appendValueInput("var", Number);
		this.appendValueInput("downer", Number)
			.appendField("∈ [");
		this.appendValueInput("upper", Number)
			.appendField(",");
		this.appendDummyInput("")
			.appendField("]");
		
	}
}

Blockly.Blocks.F_string = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.UtilsColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_F_STRING);
		
		this.appendValueInput("str", String)
			.appendField('Flash:')
			.setCheck(String);
	}
}

Blockly.Blocks.statements_timing = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.UtilsColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_STATEMENTS_TIMING);
		
		this.appendDummyInput("")
			.appendField("语句块计时 单位:")
			.appendField(new Blockly.FieldDropdown([["毫秒(ms)", "millis"], ["微秒(us)", "micros"]]),  "unit");
		this.appendStatementInput("codes");

	}
}

Blockly.Blocks.pragma_optimize = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.UtilsColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(false);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_PRAGMA_OPTIMIZE);
		
		this.appendDummyInput("")
			.appendField('启用:')
			.appendField(new Blockly.FieldDropdown([['O3', '3'], ['O2', '2']]), "arg")
			.appendField('优化');
	}
}

Blockly.Blocks.better_random_seed = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.UtilsColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_BETTER_RANDOM_SEED);
		
		this.appendDummyInput("")
			.appendField("更好的随机数初始化");
	}
}

function binary_check(str){
	var vaildStr = "";
	for(let chr of str){
		if(chr === '0' || chr === '1' || chr === ' ') vaildStr += chr;
	}
	if(vaildStr.trim() === '') vaildStr = '0';
	return vaildStr;
}

Blockly.Blocks.binary_constant = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.UtilsColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(true);
		// this.setTooltip(Blockly.TIP_BINARY_CONSTANT);
		
		this.appendDummyInput("")
			.appendField("二进制:")
			.appendField(new Blockly.FieldTextInput("11101001", binary_check), "bin_num");
	},
	onchange: function(e){
		var bin_num = this.getFieldValue("bin_num").replace(/ /g, "");
		var tip =	Blockly.TIP_BINARY_CONSTANT + 
					bin_num + 
					Blockly.TIP_BINARY_CONSTANT_DYNAMIC_TRANSLATE + 
					String(parseInt(bin_num, 2));
		this.setTooltip(tip);
	}
}

function yingXiaoHaoGenerator(body, thing, other_word){
	return Blockly.YING_XIAO_HAO_TEMPLATE.replace(/\\17/g, body).replace(/\\18/g, thing).replace(/\\19/g, other_word).replace(/\\n\"/g, '').replace(/\"/g, '');
}

Blockly.Blocks.abandon_retval = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.UtilsColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(false);
		this.setTooltip(Blockly.TIP_ABANDON_RETVAL);
	
		this.appendValueInput("valblock", null)
			.appendField("※");
		// .appendField("|");
	},
	onchange: function(e){
		var target = this.getInputTargetBlock("valblock");
		if(target != null){
			this.setColour(target.getColour());
		}
	}
}

Blockly.Blocks.yingxiaohao_generator = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.UtilsColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(false);
	//	this.setTooltip(Blockly.);
		
		this.appendDummyInput("")
			.appendField("营销号文章生成:");
			// .appendField(new Blockly.FieldDropdown([['UTF-8', 'UTF-8'], ['ANSI', 'ANSI']]), 'format');
			
		this.appendDummyInput("").appendField("主体:")
			.appendField(new Blockly.FieldTextInput('吃西瓜'),  "body");
			
		this.appendDummyInput("").appendField("事件:")
			.appendField(new Blockly.FieldTextInput('不能吐葡萄皮'),  "thing");
			
		this.appendDummyInput("").appendField("另一种说法:")
			.appendField(new Blockly.FieldTextInput('西瓜太大了, 葡萄皮吞下去容易噎着'),  "other_word");
			
		// this.appendDummyInput("result");

	},
	onchange: function(e){
		var text = yingXiaoHaoGenerator(this.getFieldValue("body"), this.getFieldValue("thing"), this.getFieldValue("other_word"));
		// this.removeInput("result");
		// this.appendDummyInput("result")
			// .appendField(text);
		this.setTooltip(text);
	}
}

Blockly.Blocks.yingxiaohao_generator_dynamic = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.UtilsColour);
		this.setOutput(true);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setInputsInline(false);

		this.appendDummyInput("")
			.appendField("营销号文章动态生成:");
			
		this.appendValueInput("body", null).appendField("主体:");
			
		this.appendValueInput("thing", null).appendField("事件:");
			
		this.appendValueInput("other_word", null).appendField("另一种说法:");
		
	},
	onchange: function(e){
		var text = yingXiaoHaoGenerator(Blockly.Arduino.valueToCode(this, 'body', Blockly.Arduino.ORDER_ATOMIC),
								 Blockly.Arduino.valueToCode(this, 'thing', Blockly.Arduino.ORDER_ATOMIC), 
								 Blockly.Arduino.valueToCode(this, 'other_word', Blockly.Arduino.ORDER_ATOMIC));
		this.setTooltip(text);
	}
}



Blockly.Blocks.block_dye = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.UtilsColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_BLOCK_DYE);
	
		this.appendDummyInput("")
			.appendField("Magic!")
			.appendField(new Blockly.FieldAngle(160), "color");
	}, 
	onchange: function(e){
		var block = this.getNextBlock();
		do{
			if(block.type == "block_dye" || block.type == "block_dye_hex") return;
			if(block.type != "abandon_retval") block.setColour(this.getFieldValue("color"));
			else if(block.getInputTargetBlock("valblock") != null)
				block.getInputTargetBlock("valblock").setColour(this.getFieldValue("color"));
			
			block = block.getNextBlock();
		}while(block);
	}	
}

Blockly.Blocks.block_dye_hex = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.UtilsColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_BLOCK_DYE_HEX);
	
		this.appendDummyInput("")
			.appendField("Hex Magic! #")
			.appendField(new Blockly.FieldTextInput("F48FB1"), "color_hex");
	}, 
	onchange: function(e){
		var block = this.getNextBlock();
		do{
			if(block.type == "block_dye" || block.type == "block_dye_hex") return;
			if(block.type != "abandon_retval") block.setColour(this.getColour());
			else  if(block.getInputTargetBlock("valblock") != null)
				block.getInputTargetBlock("valblock").setColour(this.getColour());
			
			block = block.getNextBlock();
		}while(block);
	}
}

Blockly.Blocks.warning_contagion = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.UtilsColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_WARNGING_CONTAGION);
	
		this.appendDummyInput("")
			.appendField("Warning Magic!")
			.appendField(new Blockly.FieldTextInput("噫!"), "warning_text");
	}, 
	onchange: function(e){
		var block = this.getNextBlock();
		var text = this.getFieldValue("warning_text");
		if(text == "") text = null;
		do{
			if(block.type == "warning_contagion") return;
			block.setWarningText(text, "id_warning_magic");
			block = block.getNextBlock();
		}while(block);
	}
}



var eightBtns_btnIdx_array = [["任意键", '0'], ["键1", '1'], ["键2", '2'], ["键3", '3'], ["键4", '4'], ["键5", '5'], ["键6", '6'], ["键7", '7'], ["键8", '8']];
var eightBtns_waits_array = [["被触摸", 'touched'], ["被松开", 'released'], ["被触摸直到松开", 'touched_until_released']];

//Rais_BS818A_2_Parallel
Blockly.Blocks.create_eight_buttons = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.EightButtonsColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(false);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_CREATE_EIGHT_BTNS);
		
		this.appendDummyInput("")
			.appendField("初始化8键触摸按钮");
		this.appendDummyInput("")	
			.appendField("名称:")
			.appendField(new Blockly.FieldTextInput("eightBtns"), 'eightBtns_name');
		this.appendValueInput("B0", Number)
			.setCheck(Number)
			.appendField("B0:");
		this.appendValueInput("B1", Number)
			.setCheck(Number)
			.appendField("B1:");
		this.appendValueInput("B2", Number)
			.setCheck(Number)
			.appendField("B2:");
		this.appendValueInput("B3", Number)
			.setCheck(Number)
			.appendField("B3:");
			
	}
}

Blockly.Blocks.eight_btns_get_key = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.EightButtonsColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_EIGHT_BTNS_GET_KEY);
		
		this.appendDummyInput("")
			.appendField("8键按钮")
			.appendField(new Blockly.FieldTextInput("eightBtns"), "eightBtns_name")
			.appendField("获取键值");
	}
}

Blockly.Blocks.eight_btns_isTouched = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.EightButtonsColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_EIGHT_BTNS_ISPRESSED);
		
		this.appendDummyInput("")
			.appendField("8键按钮")
			.appendField(new Blockly.FieldTextInput("eightBtns"), "eightBtns_name")
			.appendField("是否有按键被触摸");
	}
}

Blockly.Blocks.eight_btns_get_touch_time_len = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.EightButtonsColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_EIGHT_BTNS_GET_PRESS_TIME_LEN);
	
		
		this.appendDummyInput("")
			.appendField("8键按钮")
			.appendField(new Blockly.FieldTextInput("eightBtns"), "eightBtns_name")
			.appendField("获取")
			.appendField(new Blockly.FieldDropdown(eightBtns_btnIdx_array), "btnIdx")
			.appendField("从触摸到松开的时间(ms)");
			
	}
}

Blockly.Blocks.eight_btns_wait_untils = {
	init: function(){
		this.setColour(Blockly.Blocks.Rais_MixlyLib.EightButtonsColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_EIGHT_BTNS_WAITS);
	
		this.appendDummyInput("")
			.appendField("8键按钮")
			.appendField(new Blockly.FieldTextInput("eightBtns"), "eightBtns_name")
			.appendField("等待")
			.appendField(new Blockly.FieldDropdown(eightBtns_btnIdx_array), "btnIdx")
			.appendField(new Blockly.FieldDropdown(eightBtns_waits_array), "waits_sign");
		this.appendStatementInput("callback_action")
			.appendField("执行:");
			
	}
}

Blockly.Blocks.eight_btns_return = {
	init: function(){	
		this.setColour(Blockly.Blocks.Rais_MixlyLib.EightButtonsColour);
		this.setPreviousStatement(true);
		this.setNextStatement(false);
		this.setOutput(false);
		this.setInputsInline(true);
		this.setTooltip(Blockly.TIP_EIGHT_BTNS_RETURN);
		
		this.appendDummyInput("")
			.appendField("跳出「执行」域");
	},
	onchange: function(e){
		var block = this;
		do{
			if(block.type == "eight_btns_wait_untils"){
				this.setWarningText(null);
				return;
			}
			block = block.getSurroundParent();
		}while(block);
		this.setWarningText(Blockly.WARNING_EIGHT_BTNS_RETURN);
	}
		
}


//unused:
//matrix data operate:
Blockly.Blocks.create_mat_data_32x8 = {
	init: function(){
		this.setColour(	Blockly.Blocks.Rais_MixlyLib.UtilsColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(true);
	//	this.setTooltip(Blockly.);
		
		this.appendDummyInput("")
			.appendField("创建32*8点阵数据")
			.appendField('名称:')
			.appendField(new Blockly.FieldTextInput("mat_data"), 'mat_name');
	}
}
Blockly.Blocks.get_mat_data = {
	init: function(){
		this.setColour(	Blockly.Blocks.Rais_MixlyLib.UtilsColour);
		this.setPreviousStatement(false);
		this.setNextStatement(false);
		this.setOutput(true);
		this.setInputsInline(true);
	//	this.setTooltip(Blockly.);
		
		this.appendDummyInput("")
			.appendField('点阵数据');
		this.appendDummyInput("")
			.appendField(new Blockly.FieldTextInput("mat_data"), 'mat_name')
			.appendField('获取:');
		this.appendValueInput("x")
			.appendField('X:')
			.setCheck(Number);
		this.appendValueInput("y")
			.appendField('Y:')
			.setCheck(Number);
			
	}
}
Blockly.Blocks.set_mat_data = {
	init: function(){
		this.setColour(	Blockly.Blocks.Rais_MixlyLib.UtilsColour);
		this.setPreviousStatement(true);
		this.setNextStatement(true);
		this.setOutput(false);
		this.setInputsInline(true);
	//	this.setTooltip(Blockly.);
		
		this.appendDummyInput("")
			.appendField('点阵数据');
		this.appendDummyInput("")
			.appendField(new Blockly.FieldTextInput("mat_data"), 'mat_name')
			.appendField('设置:');
		this.appendValueInput("x")
			.appendField('X:')
			.setCheck(Number);
		this.appendValueInput("y")
			.appendField('Y:')
			.setCheck(Number);
		this.appendValueInput("state");
	}
}
